package com.liu.mhl.Server;

import com.liu.mhl.dao.BillDao;
import com.liu.mhl.domain.Bill;

import java.util.List;
import java.util.UUID;

public class BillServer {
    private BillDao billDao = new BillDao();
    private MenuServer menuServer = new MenuServer();
    public DiningTableServer diningTableServer =new DiningTableServer();
    //编写点餐的方法
    //1.生成账单
    //2.需要更新餐桌的状态
    public boolean orderMenu(int menuId,int nums,int diningTableId){
        //生成一个账单号，UUID
        String billId = UUID.randomUUID().toString();
        //将账单生成到bill表
        int update = billDao.update("insert into bill,values(null,?,?,?,?,?,now(),'未结账')",
                billId, menuId, nums, menuServer.getMenuById(menuId).getPrice() * nums, diningTableId);
        if(update <= 0){
            return false;
        }
        return diningTableServer.updateDiningTableState(diningTableId,"就餐中");
    }
    //返回所有的账单
    public List<Bill> list(){
        return billDao.queryMulti("select * from bill",Bill.class);
    }
    //查看某个餐桌是否有未结账的账单
    public boolean hasBillByDiningTableId(int diningTableId){
        Bill bill = billDao.querySingle("select * from bill where diningTableId = ? and state = '未结账' limit 0,1", Bill.class, diningTableId);
        return bill != null;
    }

    //完成结账{如果餐桌存在，并且餐桌处于未结账的状态}
    public boolean payBill(int diningTableId , String payMode){
        //修改bill表
        int update = billDao.update("update bill set state = ? where  diningTableId= ? and state = '未结账'", payMode, diningTableId);
        if(update <= 0){//如果更新失败，则直接返回，不执行下面语句
            return false;
        }
        //修改diningTable
        if (diningTableServer.updateDiningTableOrderState(diningTableId
        ,"空")){
            return false;
        }
        return true;
    }
}
